﻿using System.Linq;

namespace SS.Common.DBBuilder.DBDataStructure
{
    /// <summary>
    /// Class for implementing extension methods
    /// </summary>
    public static class ExtensionMethods
    {
        /// <summary>
        /// Get specified table from name
        /// </summary>
        /// <param name="db">Database</param>
        /// <param name="TblName">The name of table will be retrieved</param>
        /// <returns></returns>
        public static Table GetTable(this DataBase db, string TblName)
        {
            return db.Tables.FirstOrDefault(n => n.Name.Equals(TblName, System.StringComparison.OrdinalIgnoreCase));
        }

        /// <summary>
        /// Get primary key of the table
        /// </summary>
        /// <param name="tbl"></param>
        /// <returns></returns>
        public static Attribute GetPrimaryKey(this Table tbl)
        {
            return tbl.Attributes.FirstOrDefault(n => n.IsPrimaryKey);
        }

        /// <summary>
        /// Get attribute of the table from attribute name
        /// </summary>
        /// <param name="tbl"></param>
        /// <param name="AttrName">The name of attribute will be retrieved</param>
        /// <returns></returns>
        public static Attribute GetAttribute(this Table tbl, string AttrName)
        {
            return tbl.Attributes.FirstOrDefault(n => n.Name.Equals(AttrName, System.StringComparison.OrdinalIgnoreCase));
        }

        /// <summary>
        /// From DataType to string
        /// </summary>
        /// <param name="type"></param>
        /// <returns></returns>
        public static string ToStringValue(this DataType type)
        {
            return GetDataType(type);
        }

        /// <summary>
        /// Get DataType string
        /// </summary>
        /// <param name="type">DataType instance</param>
        /// <returns></returns>
        private static string GetDataType(DataType type)
        {
            switch (type)
            {
                case DataType.INT:
                    return "int";

                case DataType.GUILD:
                    return "Guid";

                case DataType.BOOL:
                    return "bool";

                case DataType.DATETIME:
                    return "DateTime";

                case DataType.DOUBLE:
                    return "double";

                case DataType.FLOAT:
                    return "float";

                case DataType.LONG:
                    return "long";

                case DataType.IMAGE:
                    return "byte[]";

                default:
                    return "string";
            }
        }
    }
}
